<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<div th:replace="common/admin-header.html :: adminCss"/>
<body>
    <div class="layui-form layuimini-form">
        <input type="hidden" name="id"  th:value="${sysUser.id}" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" th:value="${sysUser.username}" lay-verify="required|isExist" lay-reqtext="用户名不能为空" placeholder="请输入用户名" class="layui-input">
                <tip>填写自己管理账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="男" th:checked="${sysUser.sex==0 ? true :false}">
                <input type="radio" name="sex" value="1" title="女" th:checked="${sysUser.sex==1 ? true :false}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机</label>
            <div class="layui-input-block">
                <input type="number" name="phone" th:value="${sysUser.phone}" lay-verify="required|phone" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email" th:value="${sysUser.email}" placeholder="请输入邮箱" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职业</label>
            <div class="layui-input-block">
                <input type="text" name="work" th:value="${sysUser.work}" placeholder="请输入职业" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text" th:if="${sysUser.id == null}">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="email" name="password" th:value="${sysUser.password}" lay-verify="required|pwd" placeholder="请输入登录密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script th:inline="javascript">
        layui.use(['form','jquery'], function () {
            var form = layui.form();
            var $ = layui.jquery;
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.iframeAuto(index);
            // 表单验证
            form.verify ({
                pwd : function(value){
                     var patn = /^(\w){6,20}$/;
                     if (!patn.exec(value)){
                         return '密码必须6-20位';
                     }
                },
                isExist : function(value) {
                    var success;
                    $.ajax({
                        url: [[${path}]] + '/admin/check/isExist/user',
                        dataType: 'json',
                        type: "post",
                        async: false,
                        data: {'username': value},
                        success: function (result) {
                           success = result.success;
                        }
                    });
                    if(!success){
                        return '用户名已存在';
                    }
                }
            });
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    url: [[${path}]] + '/admin/sys/user/modify',
                    data:data.field,
                    method:'post',
                    dataType:'json',
                    success:function (result) {
                        if(result.success){
                            parent.layer.msg('提交成功', {shade: 0.3})
                            setTimeout(function () {
                                parent.test();
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            },1000);
                        }else{
                            parent.layer.msg(result.message, {shade: 0.3})
                        }
                    }
                })
                return false;
            });
        });
</script>
</body>
</html>